// pages/my/index.js
// components/echarts/echarts.js
import * as echarts from '../../ec-canvas/echarts';

var seriousData

Page({
    /**
     * 页面的初始数据
     */
    data: {
        loginFalse: false,
        AlreadyLogin: true,
        usrPic: '../../images/usrpic.jpg',
        NickName: 'NickName',
        Month: '',
        Budget: '0.00',
        Out: '',
        In: '',
        Left: '',
        LeftBudget:'',
        ec: {
            onInit: initChart
        },
        date:'',
        dateYear:''
    },
    HandleGetUserInfo(e) {
        const {userInfo} = e.detail;
        wx.setStorageSync('userinfo', userInfo);
        this.setData({
            loginFalse: true,
            AlreadyLogin: false,
            NickName: userInfo.nickName,
            usrPic: userInfo.avatarUrl
        });

        wx.login({
            success(res) {
                if (res.code) {
                    console.log(res.code)
                    //发起网络请求
                    wx.request({
                        
                        url: 'http://www.lll.plus:8088/login/',
                        method:'POST',
                        data: {
                            'name':userInfo.nickName,
                            'avatar':userInfo.avatarUrl,
                            'js_code': res.code
                        },
                        success (res1) {
                            console.log("return res1:")
                            console.log(res1.data)
                          }
                    })
                } else {
                    console.log('登录失败！' + res.errMsg)
                }
            }
        })
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
       
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        seriousData = this;
        var that = this;
        wx.getSetting({
            success (res) {
            //   console.log(res.authSetting['scope.userInfo'])
              if(res.authSetting['scope.userInfo']&&wx.getStorageSync('userinfo')){
                // console.log(res)
                const userInfo = wx.getStorageSync('userinfo');
                // console.log(userInfo)
                that.setData({
                    loginFalse: true,//true
                    AlreadyLogin: false,//false
                    NickName: userInfo.nickName,
                    usrPic: userInfo.avatarUrl
                    });
                    // console.log(that.data)
              }
              else{
                const userInfo = wx.getStorageSync('userinfo');
                that.setData({
                    loginFalse: false,//true
                    AlreadyLogin: true,//false
                    NickName: userInfo.nickName,
                    usrPic: userInfo.avatarUrl
                    });
              }
            }
          })
        
        //计算当前的年份月份并赋值给页面
        var date = new Date();
        var nYear = date.getFullYear();
        var nMonth = date.getMonth() + 1;
        that.setData({
            Month:nMonth
        })
        if (nMonth < 10) {
            nMonth = "0" + nMonth + "月";
        } else {
            nMonth = nMonth + "月";
        }
        that.setData({
            date: nMonth,
            dateYear: nYear
        })
        
        wx.request({
            url: 'http://www.lll.plus:8088/details/', //仅为示例，并非真实的接口地址
            method: 'POST',
            data: {
                uid: '1111',
                year: that.data.date,
                month: that.data.dateYear
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            dataType: 'json',
            success(res) {
                that.setData({
                    Out:parseFloat(res.data.data.pay.num).toFixed(2),
                    In:parseFloat(res.data.data.income.num).toFixed(2),
                    Left:parseFloat(parseFloat(res.data.data.pay.num).toFixed(2)-parseFloat(res.data.data.income.num).toFixed(2)).toFixed(2),
                    LeftBudget:parseFloat(that.Budget-parseFloat(res.data.data.income.num).toFixed(2)).toFixed(2)
                })
                // console.log("set data")
                // console.log(that.data)
                
            }
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})

//绘制环形图
function initChart(canvas, width, height) {
    // console.log("seriousdata")
    // console.log(seriousData.data);
    const chart = echarts.init(canvas, null, {
        width: width,
        height: height
    });
    canvas.setChart(chart);

    var option = {
        color: ['#ffc758', '#dddddd'],
        tooltip: {
            trigger: 'item'
        },
        title: {
            x: 'center',
            y: 'center',
        },
        series: [
            {
                type: 'pie',
                radius: ['40%', '70%'],
                avoidLabelOverlap: false,
                label: {
                    show: false,
                    position: 'center'
                },
                emphasis: {
                    label: {
                        show: true,
                        fontSize: '40',
                        fontWeight: 'bold'
                    }
                },
                labelLine: {
                    show: false
                },
                data: [
                    {value: seriousData.data.Budget-seriousData.data.Out, name: '预算'},
                    {value: seriousData.data.Out, name: '支出'}
                ]
            }
        ]
    };
    chart.setOption(option);
    return chart;
}